/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.edu.iuh.dhth7dlt.emailclient.utils;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import org.ini4j.Ini;

/**
 *
 * @author Friendless
 */
public class FileHelper {

    public static File[] listFileInDirectory(String directoryName) {

        //Lọc loại tệp tin muốn lấy ví dụ đuôi dat
        FilenameFilter filter = new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                // TODO Auto-generated method stub
                if (name.toLowerCase().endsWith(".dat")) {
                    return true;
                }
                return false;
            }
        };
        //Chú ý nếu thư mục không tồn tại trả về null
        return new File(directoryName).listFiles(filter);
    }

    public static ArrayList<ConfigAccount> getAccounts() throws IOException {

        File[] files = listFileInDirectory("UserData");
        ArrayList<ConfigAccount> accounts = new ArrayList<ConfigAccount>();
        for (File f : files) {
            ConfigAccount account = new ConfigAccount();
            Ini ini = new Ini(f);
            account.setName(ini.get("Account", "Name"));
            account.setEmail(ini.get("Account", "Email"));
            account.setUser(ini.get("Account", "User"));
            account.setPassword(ini.get("Account", "Password"));
            account.setPOPserver(ini.get("POP", "POPServer"));
            account.setPOPport(Integer.parseInt(ini.get("POP", "POPPort")));
            account.setSMTPserver(ini.get("SMTP", "SMTPServer"));
            account.setSMTPport(Integer.parseInt(ini.get("SMTP", "SMTPPort")));
            account.setPathDB(ini.get("Settings", "PathDB"));
            account.setSaveMailOnServer(Boolean.parseBoolean(ini.get("Settings", "SaveMailOnServer")));
            accounts.add(account);
            ini.clear();
        }
        return accounts;
    }
}
